home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / articles / att-vs-intel.txt next >
Encoding:
Text File  |  2002-01-24  |  4.9 KB  |  166 lines

  1.                Difference Between AT&T and Intel Assembly Syntax
  2.                -------------------------------------------------
  3.  
  4. The difference
  5. --------------
  6.  
  7.     This document is more related to coding than hacking, although
  8. assembly is a very useful programming language, as its machine level and
  9. provides direct access to the CPU, hardware, etc. Now in all Unix-derived 
  10. systems, the compilers like gcc use att syntax assembly and not intel. For
  11. example: movl %esp, %ebp
  12.  
  13. Now this is unfortunate for DOS assembly programmers who recently
  14. switched to Unix-derived systems. They are used to Intel syntax, whereas
  15. Linux (and others) uses AT&T syntax. Where in the example above you
  16. would use: mov ebp, esp.
  17.  
  18.     I wrote this because I have only seen one document that
  19. explained the differences between AT&T and Intel syntax. That document was
  20. the GAS (GNU assembler) reference manual.  
  21.  
  22. You can get the GAS reference manual at:
  23. http://www.cs.utah.edu/csinfo/texinfo under "gas"
  24.  
  25. First let me give a few examples. 
  26. Intel: push 4
  27. AT&T: pushl $4
  28.  
  29. All the immediate operands have a $ in front of them, in intel syntax, you
  30. don't have prefix.
  31.  
  32. The register operands, have a % in front of them, intel has none.
  33. Intel: mov eax, 4
  34. att: movl $4, %eax
  35.  
  36. You notice there is a diff in intel/att's src/dst...
  37. Intel: you do dst, src like mov ax, 2  
  38. att: it's the opposite, src, dst like movl $2, %ax
  39.  
  40. You can use 'b' for byte, 'w' for word, 'l' for long, etc...as the memory
  41. suffix:
  42. movl, movb, movw, etc.
  43. in intel you wold do this like mov ax, byte ptr foo...
  44.  
  45. The far instruction for att is lret $stack-adjust, in intel it's 
  46. ret far stack-adjust.
  47.  
  48. The l in front of mov, is the byte/memory operand..... this is actually
  49. more convient if you ask me.
  50.  
  51. In Intel you have:
  52. section:[base + index*scale + disp]
  53.  
  54. disp = displcement
  55. scale = 1 if not given
  56.  
  57. In AT&T, however, you would have:
  58. section:disp(base, index, scale)
  59.  
  60. So "es:[ebp-5]" in Intel would be "%es:-4(%ebp)" in AT&T syntax.
  61.  
  62. Intel: [foo]  AT&T: foo(,1) the ,1 means an index of one...
  63. Inte: [foor + eax*4] AT&T: foor(, %eax, 4)
  64.  
  65. I hope this helps :)
  66.  
  67. How to Get some assembly examples in unix:
  68. -----------------------------------------
  69.      
  70. Now how to get a few examples on how to get some assembly code for Unix.
  71. Use this (assuming you called it test.c):
  72.  
  73. void main()
  74. {
  75.   printf("hi\n");
  76. }
  77.  
  78. now to compile it, do gcc -S test.c, this will make a file test.s in
  79. assembly......look at it it contains great info....and some examples of
  80. the macros and what not defined/shown in gas' (GNU assembler) manual.
  81. (Which can be found at http://www.cs.utah.edu/csinfo/texinfo, under gas.
  82.  
  83. here is what test.s will look like:
  84.  
  85.         .file   "test.c"
  86.         .version        "01.01"
  87. gcc2_compiled.:
  88. .section        .rodata
  89. .LC0:
  90.         .string "test\n"
  91. .text
  92.         .align 4
  93. .globl main
  94.         .type    main,@function
  95. main:
  96.         pushl %ebp
  97.         movl %esp,%ebp
  98.         pushl $.LC0
  99.         call printf
  100.         addl $4,%esp
  101. .L1:
  102.         leave
  103.         ret
  104. .Lfe1:
  105.         .size    main,.Lfe1-main
  106.         .ident  "GCC: (GNU) 2.7.2.1"
  107.  
  108.  
  109. As you know, the l's in front of push, mov, add, etc....that means it's
  110. type long. and the % goes in front of all register operands, whereas in
  111. intel syntax, it is undelimited. Likewise, the immediate operands, have a
  112. '$' in front of them, whereas once again, intel is undelimited.
  113.  
  114. movl $3, %eax 
  115. is equal to:
  116. mov eax, 3 
  117. in intel
  118.  
  119. The other way to get asm code is with gdb......you compile your program
  120. with gcc -g .......and for even more......gcc -g -a...
  121. here is our test.c ......in gdb,
  122. we do 'disassemble main':
  123.  
  124. (gdb) disassemble main
  125. Dump of assembler code for function main:
  126. 0x8048474 <main>:       pushl  %ebp
  127. 0x8048475 <main+1>:     movl   %esp,%ebp
  128. 0x8048477 <main+3>:     pushl  $0x80484c8
  129. 0x804847c <main+8>:     call   0x8048378 <printf>
  130. 0x8048481 <main+13>:    addl   $0x4,%esp
  131. 0x8048484 <main+16>:    leave  
  132. 0x8048485 <main+17>:    ret    
  133. End of assembler dump.
  134.  
  135. That is with just -g.......with -a as well you can see the difference
  136. (more instructions show up that usually wouldn't):
  137.  
  138. (gdb) disassemble main
  139. Dump of assembler code for function main:
  140. 0x80485d8 <main>:       pushl  %ebp
  141. 0x80485d9 <main+1>:     movl   %esp,%ebp
  142. 0x80485db <main+3>:     cmpl   $0x0,0x8049a6c
  143. 0x80485e2 <main+10>:    jne    0x80485f1 <main+25>
  144. 0x80485e4 <main+12>:    pushl  $0x8049a6c
  145. 0x80485e9 <main+17>:    call   0x80488fc <__bb_init_func>
  146. 0x80485ee <main+22>:    addl   $0x4,%esp
  147. 0x80485f1 <main+25>:    incl   0x8049b78
  148. 0x80485f7 <main+31>:    pushl  $0x8048978
  149. 0x80485fc <main+36>:    call   0x8048468 <printf>
  150. 0x8048601 <main+41>:    addl   $0x4,%esp
  151. 0x8048604 <main+44>:    incl   0x8049b7c
  152. 0x804860a <main+50>:    leave  
  153. 0x804860b <main+51>:    ret    
  154. End of assembler dump.
  155.  
  156. I of course need to give credit of this to the gas manual, as parts were
  157. taken from there.
  158.  
  159.  
  160.                       Shok (Matt Conover)
  161.  
  162. Email: shok@dataforce.net
  163. Web:   http://www.w00w00.org
  164. ----------------
  165.  
  166.